﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Zor_Modes;
using Zor_Tools;
using Zor_Bll;
using Newtonsoft.Json.Linq;

namespace Zor_API.Controllers
{
    public class PropertyInfoController : ApiController
    {
        protected List<PropertyInfo> StreetOfficeList = null;
        /// <summary>
        /// 房产信息添加
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage InserPropertyInfo([FromBody]PropertyInfo pi)
        {
            pi.AuditStatus = 0;
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> propertyInfo = PropertyInfo_Bll.GetPropertyInfoList(" and Bd_Id=" + pi.Bd_Id + " and Uim_Id=" + pi.Uim_Id + " and Hs_Id=" + pi.Hs_Id + " and AuditStatus=" + pi.AuditStatus);
            if (propertyInfo.Count > 0)
            {
                bar.AnswerType = BaseAnswer.Answers.Failure;
                bar.AnswerMeg = "当前房屋已存在未审核信息！";
            }
            else
            {
                bar = PropertyInfo_Bll.InserPropertyInfo(pi);
            }
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

        /// <summary>
        /// 获取街办信息
        /// </summary>
        /// <returns>返回Dt_ParentId街办ID，Dt_ParentName街办名称</returns>
        [HttpPost]
        public List<PropertyInfo> StreetOffice()
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> streetOfficeList = PropertyInfo_Bll.GetStreetOffice();
            StreetOfficeList = streetOfficeList;
            return streetOfficeList;
        }

        /// <summary>
        /// 根据街办ID获取社区信息
        /// </summary>
        /// <param name="pi">pi.Dt_ParentId</param>
        /// <returns>返回Dt_Id社区ID,Dt_Name社区姓名</returns>
        [HttpPost]
        public List<PropertyInfo> CommunityByStreetOfficeID([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> communityList = PropertyInfo_Bll.GetCommunityByStreetOfficeID(pi.Dt_ParentId);
            return communityList;
        }

        /// <summary>
        /// 根据社区ID获取楼宇信息
        /// </summary>
        /// <param name="communityID">社区Dt_Id</param>
        /// <returns>返回Bd_Id楼宇ID,Bd_Name楼宇姓名</returns>
        [HttpPost]
        public List<PropertyInfo> BuildingByCommunityID([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> buildingList = PropertyInfo_Bll.GetBuildingByCommunityID(pi.Dt_Id);
            return buildingList;
        }


        /// <summary>
        /// 根据楼宇ID获取单元信息
        /// </summary>
        /// <param name="buildingID">楼宇Bd_Id</param>
        /// <returns>返回Uim_Id单元ID,Uim_Name单元姓名</returns>
        [HttpPost]
        public List<PropertyInfo> UnitInforByBuildingID([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> unitInforList = PropertyInfo_Bll.GetUnitInforByBuildingID(pi.Bd_Id);
            return unitInforList;
        }

        /// <summary>
        /// 根据单元ID获取房屋信息
        /// </summary>
        /// <param name="Uim_Id">单元Uim_Id</param>
        /// <returns>返回Hs_ID房屋ID,Hs_Number房屋名称</returns>
        [HttpPost]
        public List<PropertyInfo> HouseByUnitInforID([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> houseList = PropertyInfo_Bll.GetHouseByUnitInforID(pi.Uim_Id);
            return houseList;
        }

        /// <summary>
        /// 获取推送消息集合。根据用户所属街办
        /// </summary>
        /// <param name="pi"></param>
        [HttpPost]
        public List<PropertyInfo> PushMegList([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> lstpi = PropertyInfo_Bll.GetPushMegList(pi);
            return lstpi;
        }

        /// <summary>
        /// 获取推送消息集合。根据用户所属街办
        /// </summary>
        /// <param name="ids">身份证号</param>
        [HttpPost]
        public List<PropertyInfo> PushMegListByUserId([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> lstpi = PropertyInfo_Bll.GetPushMegListByUserId(pi);
            return lstpi;
        }

        /// <summary>
        /// 删除推送消息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage DelPushMeg([FromBody]JObject ids)
        {
            dynamic json = ids;

            JArray jnews = json.ids;
            List<int> lst = new List<int>();
            foreach (var item in jnews)
            {
                lst.Add(item.ToObject<PropertyInfo>().Property_Id);
            }

            BaseAnswer bar = PropertyInfo_Bll.DelPushMeg(lst.ToArray());
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result; 

        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage ApplyPushMeg([FromBody]PropertyInfo pi)
        {
            pi.AuditTime = DateTime.Now;
            BaseAnswer bar = PropertyInfo_Bll.ApplyPushMeg(pi);
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result; 
        }
    }
}
